package com.zhou.utils;

import net.sourceforge.pinyin4j.PinyinHelper;

import java.util.Comparator;


public  class PinyinUtils implements Comparator<String> {
    @Override
    public int compare(String o1, String o2) {
        return convertToHanYuPinYinString(o1).compareTo(convertToHanYuPinYinString(o2));
    }

    private String convertToHanYuPinYinString(String str){

        StringBuilder sb=new StringBuilder();
        String[] arr=null;

        for(int i=0;i<str.length();i++){
            arr= PinyinHelper.toHanyuPinyinStringArray(str.charAt(i));
            if(arr!=null && arr.length>0){
                for (String string : arr) {
                    sb.append(string);
                }
            }
        }

        return sb.toString();
    }
}
